/*
 * Copyright (c) 2012 Supin P Surendran <supinps@gmail.com>.
 *
 * This file is part of InventoryMan.
 *
 * InventoryMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * InventoryMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with licenseTest.  If not, see <http ://www.gnu.org/licenses/>.
 *
 */
package InventoryMan;

/**
 *
 * @author supinps
 */
public class ItemModify extends javax.swing.JInternalFrame {

    /** Creates new form ItemModify
     * @param conn
     */
    public ItemModify(java.sql.Connection conn) {
        imConnection=conn;//Connection to the database
        itemNames=new javax.swing.DefaultComboBoxModel();//ComboBox model that holds data for the Combobox
        initComponents();
        initDataSet();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TxtPrice = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        TxtLength = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        TxtWidth = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TxtCode = new javax.swing.JTextField();
        BnCancel = new javax.swing.JButton();
        BnSave = new javax.swing.JButton();
        TxtCess = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        TxtVat = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        CmbName = new javax.swing.JComboBox();

        setTitle("Modify Item");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/icon_small.png"))); // NOI18N
        setPreferredSize(new java.awt.Dimension(576, 249));

        jLabel5.setText("Price :");

        jLabel4.setText("Length :");

        jLabel3.setText("Width :");

        jLabel2.setText("Name :");

        TxtCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TxtCodeActionPerformed(evt);
            }
        });

        BnCancel.setText("Cancel");
        BnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnCancelActionPerformed(evt);
            }
        });

        BnSave.setText("Save");
        BnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnSaveActionPerformed(evt);
            }
        });

        jLabel6.setText("VAT :");

        jLabel7.setText("cess. :");

        jLabel1.setText("Code :");

        CmbName.setModel(itemNames);
        CmbName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CmbNameActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(TxtPrice, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TxtCode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(TxtVat, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                    .addComponent(TxtWidth, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(TxtLength)
                                    .addComponent(TxtCess, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                            .addComponent(CmbName, 0, 300, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(BnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                        .addGap(82, 82, 82)
                        .addComponent(BnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(CmbName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TxtWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(TxtLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(TxtVat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(TxtCess, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BnSave)
                            .addComponent(BnCancel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(TxtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(72, 72, 72)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TxtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnCancelActionPerformed
        this.dispose();
}//GEN-LAST:event_BnCancelActionPerformed

    private void BnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnSaveActionPerformed
        try {
            prepStmt = imConnection.prepareStatement("UPDATE item SET name=?,width=?,length=?,rate=?,vat=?,cess=? WHERE code=?");
            prepStmt.setString(1, CmbName.getSelectedItem().toString());
            prepStmt.setInt(2, Integer.parseInt(TxtWidth.getText()));
            prepStmt.setInt(3, Integer.parseInt(TxtLength.getText()));
            prepStmt.setInt(4, Integer.parseInt(TxtPrice.getText()));
            prepStmt.setInt(5, Integer.parseInt(TxtVat.getText()));
            prepStmt.setInt(6, Integer.parseInt(TxtCess.getText()));
            prepStmt.setInt(7, Integer.parseInt(TxtCode.getText()));
            prepStmt.executeUpdate();
            this.dispose();
        } catch (java.sql.SQLException ex) {
            System.out.println(ex.toString());
            javax.swing.JOptionPane.showMessageDialog(this,"Communication Error!\n"+ex.toString());
        }
}//GEN-LAST:event_BnSaveActionPerformed

    private void CmbNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CmbNameActionPerformed
        try {
            //Moving dbCursor to the Chosen Item
            imReply.absolute(CmbName.getSelectedIndex()+1);
            //Data Initialization
            code=Integer.toString(imReply.getInt("code"));
            width=Integer.toString(imReply.getInt("width"));
            length=Integer.toString(imReply.getInt("length"));
            price=Integer.toString(imReply.getInt("rate"));
            vat=Integer.toString(imReply.getInt("vat"));
            cess=Integer.toString(imReply.getInt("cess"));
            //Initialization Finished
            //Populating Form Fields
            TxtCode.setText(code);
            TxtWidth.setText(width);
            TxtLength.setText(length);
            TxtPrice.setText(price);
            TxtVat.setText(vat);
            TxtCess.setText(cess);
        } catch (java.sql.SQLException ex) {
           javax.swing.JOptionPane.showMessageDialog(this, "Connection Error\n"+ex.toString());
        }
    }//GEN-LAST:event_CmbNameActionPerformed

    private void TxtCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TxtCodeActionPerformed
        if(TxtCode.isFocusOwner()) {
        code=TxtCode.getText();
        try {
            imReply.beforeFirst();
            while(imReply.next()) {
                if(imReply.getInt("code")==Integer.parseInt(code)) {
                    //Data Initialization
                    name=imReply.getString("name");
                    width=Integer.toString(imReply.getInt("width"));
                    length=Integer.toString(imReply.getInt("length"));
                    price=Integer.toString(imReply.getInt("rate"));
                    vat=Integer.toString(imReply.getInt("vat"));
                    cess=Integer.toString(imReply.getInt("cess"));
                    //Initialization Finished
                    //Populating Form Fields
                    itemNames.setSelectedItem(name);
                    TxtCode.setText(code);
                    TxtWidth.setText(width);
                    TxtLength.setText(length);
                    TxtPrice.setText(price);
                    TxtVat.setText(vat);
                    TxtCess.setText(cess);
                }
            }
        } catch (java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "Communication Error!\n"+ex.toString());
        }
        }
    }//GEN-LAST:event_TxtCodeActionPerformed
    //Manually Defined Methods
    @SuppressWarnings("static-access")
    private void initDataSet() {
        try {
            itemSuggest.decorate(CmbName);
            stmt = imConnection.createStatement();
            imReply=stmt.executeQuery("SELECT * FROM item");
            while(imReply.next()) {
                itemNames.addElement(imReply.getString("name"));
            }
        } catch (java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "Communication Error !\n"+ex.toString());
        }

    }
    //End of Method definitions

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BnCancel;
    private javax.swing.JButton BnSave;
    private javax.swing.JComboBox CmbName;
    private javax.swing.JTextField TxtCess;
    private javax.swing.JTextField TxtCode;
    private javax.swing.JTextField TxtLength;
    private javax.swing.JTextField TxtPrice;
    private javax.swing.JTextField TxtVat;
    private javax.swing.JTextField TxtWidth;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    // End of variables declaration//GEN-END:variables

    //Manually Declared Variables
    java.sql.Connection imConnection; //Connection to Database
    java.sql.PreparedStatement prepStmt;
    java.sql.Statement stmt;//Statement to fetch the item's list
    java.sql.ResultSet imReply;//reply containing item lists
    javax.swing.DefaultComboBoxModel itemNames;
    org.jdesktop.swingx.autocomplete.AutoCompleteDecorator itemSuggest;
    //Declaration of Data Holders
    String code,name,width,length,price,vat,cess;
    //End of Declarations
}
